const mysql = require('mysql');
const pool = mysql.createPool({
    host: '127.0.0.1',
    user: 'root',
    password: 'root',
    database: 'tttt',
    connectionLimit: 20,
    timeout: 1000 * 20
})
//封装promis方法
const exec = (sql, params) => {
    return new Promise((resolve, reject) => {
        pool.getConnection((err, conn) => {
            if (err) {
                console.log("连接数据库失败" + err.sqlMessage);
                reject(err)
            }
            conn.query(sql, params, (err, result) => {
                if (err) {
                    console.log("执行sql语句失败" + err.sqlMessage);
                    reject(err)
                }
                resolve(result);
                conn.release()//释放连接
            })
        })
    })
}
module.exports = { exec }